package com.smart.community.feign.property.fallback;

import java.util.Collections;
import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Component;

import com.smart.community.feign.common.BaseFeignFallbackFactory;
import com.smart.community.commons.vo.property.PropertyInfoListVO;
import com.smart.community.commons.vo.property.PropertyInfoVO;
import com.smart.community.feign.property.service.IUserPropertyCompanyFeign;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户物业公司关联Feign降级处理类
 * 
 * 当用户物业公司关联服务不可用时，提供降级处理：
 * 1. 返回安全的默认值，避免调用方异常
 * 2. 记录详细的错误日志，便于问题排查
 * 3. 确保系统稳定性，避免级联故障
 * 
 * @author Wu.Liang
 * @version 2.0.0
 * @since 2025-06-29
 * @updated 2025-08-28 - 继承BaseFeignFallbackFactory，使用统一的降级处理标准
 */
@Slf4j
@Component
public class UserPropertyCompanyFeignFallback extends BaseFeignFallbackFactory<IUserPropertyCompanyFeign> {
	@Override
	public IUserPropertyCompanyFeign create(Throwable cause) {
		// 使用基类的统一日志记录方法，区分异常类型
		if (isServiceUnavailable(cause)) {
			logServiceUnavailable(getServiceName(), cause);
		} else {
			log.warn("{}服务调用异常，执行降级处理: {}", getServiceName(), cause.getMessage());
		}
		
		return new IUserPropertyCompanyFeign() {
			@Override
			public PropertyInfoVO getPropertyInfoByUserId(Long userId) {
				logMethodFallback("getPropertyInfoByUserId", userId);
				return null; // 返回null，表示未找到
			}

			@Override
			public PropertyInfoListVO getPropertyInfoListByUserId(Long userId) {
				logMethodFallback("getPropertyInfoListByUserId", userId);
				PropertyInfoListVO result = new PropertyInfoListVO();
				result.setUserId(userId);
				result.setPropertyCompanies(Collections.emptyList());
				result.setCount(0);
				result.setHasPropertyCompany(false);
				result.setDefaultPropertyCompany(null);
				return result; // 返回空列表，不影响业务
			}

			@Override
			public PropertyInfoVO getUsersByPropertyCompanyId(Long propertyCompanyId) {
				logMethodFallback("getUsersByPropertyCompanyId", propertyCompanyId);
				return null; // 返回null，表示未找到
			}

			@Override
			public Boolean checkUserHasPropertyCompany(Long userId) {
				logMethodFallback("checkUserHasPropertyCompany", userId);
				return Boolean.FALSE; // 返回false，表示没有关联物业公司
			}

			@Override
			public List<Long> getUserIdsByPropertyCompanyId(@NotNull Long propertyCompanyId) {
				logMethodFallback("getUserIdsByPropertyCompanyId", propertyCompanyId);
				return Collections.emptyList(); // 返回空列表，不影响业务
			}
		};
	}
	
	@Override
	protected String getServiceName() {
		return "用户物业公司关联服务";
	}
}